﻿using System;

namespace BO.Management
{
    /// <summary>
    /// Provide entries to logon to the BOE platform
    /// </summary>
    public class LogonProvider : IDisposable
    {
        private const String CONNECT_SECENTERPRISE = "secEnterprise";
        private const String CONNECT_SECWINDOWSAD = "secWinAD";

        /// <summary>
        /// Get a session opened by one of available logon methods
        /// </summary>
        public CrystalDecisions.Enterprise.EnterpriseSession Session
        {
            get;
            private set;
        }

        /// <summary>
        /// Logon on BOE using Enterpirse security
        /// </summary>
        /// <param name="userName">Enterprise User name</param>
        /// <param name="password">Password</param>
        /// <param name="cmsName">System name</param>
        /// <returns></returns>
        public CrystalDecisions.Enterprise.EnterpriseSession Logon(String userName, String password, String cmsName)
        {
            using (CrystalDecisions.Enterprise.SessionMgr _sessionManager = new CrystalDecisions.Enterprise.SessionMgr())
            {
                this.Session = _sessionManager.Logon(userName, password, cmsName, CONNECT_SECENTERPRISE);
                return this.Session;
            }
        }

        /// <summary>
        /// Logon on BOE using Enterpirse security
        /// </summary>
        /// <param name="cmsName">System name</param>
        /// <returns></returns>
        public CrystalDecisions.Enterprise.EnterpriseSession Logon(String cmsName)
        {
            using (CrystalDecisions.Enterprise.SessionMgr _sessionManager = new CrystalDecisions.Enterprise.SessionMgr())
            {
                this.Session = _sessionManager.Logon(null, null, cmsName, CONNECT_SECWINDOWSAD);
                return this.Session;
            }
        }


        /// <summary>
        /// Logon on BOE using Enterpirse security
        /// </summary>
        /// <param name="token">Previous connection token</param>
        /// <returns></returns>
        public CrystalDecisions.Enterprise.EnterpriseSession LogonWithToken(String token)
        {
            using (CrystalDecisions.Enterprise.SessionMgr _sessionManager = new CrystalDecisions.Enterprise.SessionMgr())
            {
                this.Session = _sessionManager.LogonWithToken(token);
                return this.Session;
            }
        }



        public void Dispose()
        {
            if (this.Session != null) this.Session.Dispose();
        }
    }
}
